<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/7/15
 * Time: 14:11
 */

namespace app\admin\controller;


use app\admin\model\image_video\AlbumM;
use app\admin\model\image_video\ArticleM;
use app\admin\model\image_video\MottoM;
use app\admin\model\TagM;
use think\Config;
use think\File;

class ImageVideo extends Base
{

    protected $tagM;
    protected $albumM;
    protected $mottoM;
    protected $articleM;

    protected $albumTag;
    protected $articleTag;

    /*
     * 依赖注入Model
     * */
    public function __construct(TagM $tagM,AlbumM $albumM,MottoM $mottoM,ArticleM $articeM){
        parent::__construct();
        $this->tagM = $tagM;
        $this->albumM = $albumM;
        $this->mottoM = $mottoM;
        $this->articleM = $articeM;
        $this->albumTag = array_keys(Config::get('app.tag.category'))[1];
        $this->articleTag = array_keys(Config::get('app.tag.category'))[2];
    }


    /**
     * 列表
     */
    public function article_list(){
        if ($this->request->isPost()|| input('get.export/b')){
            return $this->articleM->listData();
        }
        $this->assign("__search",$this->articleM->__search());
        $this->assign("__header",$this->articleM->__header());
        return $this->fetch('image_video/article/list');
    }

    /**
     * 新增
     * 修改
     * @return mixed
     */
    public function article_form(){
        if ($this->request->isPost()) {
            $method = input('post.method/s');
            $validate = $this->articleM->validate_model();
            if ($validate['code']){
                if ($method == 'edit') {
                    return $this->articleM->edit();
                }else if ($method == 'add') {
                    return $this->articleM->add();
                }
            }else{
                return $validate;
            }
            return op_fail();
        }
        $this->assign("tags",$this->tagM->listAll($this->articleTag));
        $this->assign("lang_field",Config::get('lang_field_'.$this->curr_lang));
        $this->assign("method",input('get.method/s'));
        $this->assign("data",$this->articleM->getById(input('get.id/d')));
        return $this->fetch('image_video/article/form');
    }

    /**
     * 删除
     */
    public function article_del(){
        return $this->articleM->del();
    }

    /**
     * 列表
     */
    public function motto_list(){
        if ($this->request->isPost()|| input('get.export/b')){
            return $this->mottoM->listData();
        }
        $this->assign("__search",$this->mottoM->__search());
        $this->assign("__header",$this->mottoM->__header());
        return $this->fetch('image_video/motto/list');
    }

    /**
     * 新增
     * 修改
     * @return mixed
     */
    public function motto_form(){
        if ($this->request->isPost()) {
            $data = input('post.');
            $validate = $this->mottoM->validate_model();
            if ($validate['code']){
                $dir_date = date('Y-m');
                $files = request()->file('image');
                if (is_array($files)) {
                    $pic_urls = [];
                    $pic_ab_urls = [];
                    /**
                     * @var File $file
                     */
                    foreach ($files as $file) {
                        if ($file) {
                            $path = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'motto' . DS . $dir_date . DS;
                            file_exists($path) ?: mkdir($path, 0755, true);
                            $url = '/uploads/motto/' .$dir_date . '/';
                            $info = $file->validate(['ext' => 'gif,jpg,jpeg,bmp,png'])->rule('uniqid')->move($path);
                            if ($info) {
                                $pic_name = $info->getFilename();
                                $pic_ab_urls[] = $path . $info->getSaveName();
                                $pic_urls[] = $url . $pic_name;
                                unset($data['image']);
                            }
                        }
                    }
                    if (count($pic_urls) && count($pic_ab_urls)) {
                        $data['pic_urls'] = implode(';',$pic_urls);
                        $data['pic_ab_urls'] = implode(';',$pic_ab_urls);
                        $this->request->post($data);
                    }
                }
                if ($data['method'] == 'edit') {
                    return $this->mottoM->edit();
                }else if ($data['method'] == 'add') {
                    return $this->mottoM->add();
                }
            }else{
                return $validate;
            }
            return op_fail();
        }
        $this->assign("lang_field",Config::get('lang_field_'.$this->curr_lang));
        $this->assign("method",input('get.method/s'));
        $this->assign("data",$this->mottoM->getById(input('get.id/d')));
        return $this->fetch('image_video/motto/form');
    }

    /**
     * 删除
     */
    public function motto_del(){
        return $this->mottoM->del();
    }

    /**
     * @return mixed
     */
    public function album_thumb(){
        $tags = $this->tagM->listAll($this->albumTag);
        //每个分类相册的第一张图片做封面
        $thumb = [];
        foreach ($tags as $value){
            $code = $value['code'];
            $t = $this->albumM->getAlbumThumb($code);
            if ($t['count']){
                $thumb[$code]['url'] = $t['data']['url'];
                $thumb[$code]['count'] = $t['count'];
            }else{
                $thumb[$code]['url'] = Config::get("app.defultThumb");
                $thumb[$code]['count'] = 0;
            }
        }
        $this->assign("thumb",$thumb);
        $this->assign("tags",$tags);
        $this->assign("lang_other",Config::get('lang_other_'.$this->curr_lang));
        return $this->fetch('image_video/album/thumb');
    }

    /**
     * @return mixed
     */
    public function album_list(){
        $tag=input('get.code/s');
        $this->assign("__search",$this->albumM->__search());
        $this->assign("code",$tag);
        $this->assign("data",$this->albumM->listAllByTag($tag));
        return $this->fetch('image_video/album/list');
    }

    /*
        * 新增
        * */
    public function album_form(){
        if($this->request->isPost()) {
            $data = input('post.');
            $validate = $this->albumM->validate_model();
            if ($validate['code']) {
                $allData = [];
                $method = $data['method'];
                unset($data['method']);
                $files = request()->file('image');
                if (is_array($files)) {
                    $dir_date = date('Y-m');
                    /**
                     * @var File $file
                     */
                    foreach ($files as $file) {
                        if ($file) {
                            $path = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'album' . DS . $data['tag'] . DS . $dir_date .DS;
                            file_exists($path) ?: mkdir($path, 0755, true);
                            $url = '/uploads/album/' . $data['tag'] . '/'.$dir_date.'/';
                            $info = $file->validate(['ext' => 'gif,jpg,jpeg,bmp,png'])->rule('uniqid')->move($path);
                            if ($info) {
                                $data['name'] = $info->getFilename();
                                $data['ab_url'] = $path . $info->getSaveName();
                                $data['url'] = $url . $data['name'];
                                unset($data['image']);
                                if ($method == 'add'){
                                    $data['created_at'] = time();
                                    unset($data['id']);
                                }
                                $data['updated_at'] = time();
                                array_push($allData, $data);
                            }
                        }
                    }
                }
                if (count($allData) && $method == 'add')
                    return $this->albumM->add($allData);
                else if ($method == 'edit')
                    return $this->albumM->edit($data);
            }
            return $validate;
        }
        $this->assign("tag",input('get.code/s'));
        $this->assign("tags",$this->tagM->listAll($this->albumTag));
        $this->assign("method",input('get.method/s'));
        $this->assign("lang_field",Config::get('lang_field_'.$this->curr_lang));
        $this->assign("data",$this->albumM->getById(input('get.id/d')));
        return $this->fetch('image_video/album/form');
    }

    /*
    * 删除
    * */
    public function album_del(){
        return $this->albumM->del();
    }

    /**
     * 列表
     */
    public function tag_list(){
        if ($this->request->isPost()|| input('get.export/b')){
            return $this->tagM->listData();
        }
        $this->assign("__search",$this->tagM->__search());
        $this->assign("__header",$this->tagM->__header());
        return $this->fetch('image_video/tag/list');
    }

    /**
     * 新增
     * 修改
     * @return mixed
     */
    public function tag_form(){
        if ($this->request->isPost()) {
            $change = input('post.change/b');
            if ($change) {
                return $result=[
                    'code'=>1,
                    'data'=> $this->tagM->getLastSort(input('post.category/s'))
                ];
            }else {
                $method = input('post.method/s');
                $validate = $this->tagM->validate_model($method);
                if ($validate['code']){
                    if ($method == 'edit') {
                        return $this->tagM->edit();
                    }else if ($method == 'add') {
                        return $this->tagM->add();
                    }
                }else{
                    return $validate;
                }
            }
            return op_fail();
        }
        $this->assign("category_list",Config::get('app.tag.category'));
        $this->assign("lang_field",Config::get('lang_field_'.$this->curr_lang));
        $this->assign("method",input('get.method/s'));
        $this->assign("data",$this->tagM->getById(input('get.id/d')));
        $this->assign("lastSort",$this->tagM->getLastSort(array_keys(Config::get('app.tag.category'))[0]));
        return $this->fetch('image_video/tag/form');
    }

    /**
     * 删除
     */
    public function tag_del(){
        return $this->tagM->del();
    }


}